package com.example.shishaolong.warehousemanager.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.shishaolong.warehousemanager.common.DateUtil;
import com.example.shishaolong.warehousemanager.db.WarehouseDBHelper;
import com.example.shishaolong.warehousemanager.entity.GoodsEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 货物数据访问层
 * @author 时少龙
 */
public class GoodsDao {

    private WarehouseDBHelper helper = null;

    public GoodsDao(Context context) {
        //在构造方法中实例化的好处是，
        //当实例化GoodsDao时，数据库DbHelper也进行了实例化
        helper = new WarehouseDBHelper(context, "warehouse.db", null, 1);
    }

    /**
     * 添加货物的方法
     * @param goodsEntity
     */
    public void add(GoodsEntity goodsEntity) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("insert into goods_info(name,receive_person,describe,buyer_name,storage_date, out_date,modify_date) " +
                        "values(?,?,?,?,?,?,?)",
                new Object[]{goodsEntity.getName(),goodsEntity.getReceivePerson(),goodsEntity.getDescribe(),goodsEntity.getBuyerName(),
                goodsEntity.getStorageDate(),goodsEntity.getOutDate(),DateUtil.getCurrentDateStr()});
        db.close();
    }

    /**
     * @param id 通过id删除物品
     */
    public void delete(int id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("delete from goods_info where id=?", new Object[]{id});
        db.close();
    }

    /**
     * 删除全部物品
     */
    public void deleteAll() {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("delete from goods_info");
        db.close();
    }

    /**
     * 更新物品信息
     *
     * @param goodsEntity 物品实体类
     */
    public void update(GoodsEntity goodsEntity) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("update goods_info set name=?,receive_person=?,describe=?,buyer_name=?,storage_date=?, out_date=?," +
                        "modify_date=? where id =?",
                new Object[]{goodsEntity.getName(),goodsEntity.getReceivePerson(),goodsEntity.getDescribe(),goodsEntity.getBuyerName(),
                        goodsEntity.getStorageDate(),goodsEntity.getOutDate(),DateUtil.getCurrentDateStr(),goodsEntity.getId()});
        db.close();
    }

    /**
     * 根据id查询物品信息
     *
     * @param id
     * @return
     */
    public GoodsEntity findById(int id) {
        GoodsEntity goodsEntity = null;
        SQLiteDatabase db = helper.getReadableDatabase();

        Cursor cursor = db.rawQuery("select * from goods_info where id=?",
                new String[]{"" + id});
        if (cursor.moveToFirst()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String receivePerson = cursor.getString(cursor.getColumnIndex("receive_person"));
            String describe = cursor.getString(cursor.getColumnIndex("describe"));
            String buyerName = cursor.getString(cursor.getColumnIndex("buyer_name"));
            String storageDate = cursor.getString(cursor.getColumnIndex("storage_date"));
            String outDate = cursor.getString(cursor.getColumnIndex("out_date"));
            String modifyDate = cursor.getString(cursor.getColumnIndex("modify_date"));

            goodsEntity = new GoodsEntity();
            goodsEntity.setId(id);
            goodsEntity.setName(name);
            goodsEntity.setReceivePerson(receivePerson);
            goodsEntity.setDescribe(describe);
            goodsEntity.setBuyerName(buyerName);
            goodsEntity.setStorageDate(storageDate);
            goodsEntity.setOutDate(outDate);
            goodsEntity.setModifyDate(modifyDate);
            goodsEntity.setChecked(false);
        }

        db.close();
        return goodsEntity;
    }

    /**
     * 查询全部物品信息
     * @return
     */
    public List<GoodsEntity> findAll() {
        List<GoodsEntity> list = new ArrayList<>();
        SQLiteDatabase db = helper.getReadableDatabase();

        Cursor cursor = db.rawQuery("select * from goods_info ", null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String receivePerson = cursor.getString(cursor.getColumnIndex("receive_person"));
            String describe = cursor.getString(cursor.getColumnIndex("describe"));
            String buyerName = cursor.getString(cursor.getColumnIndex("buyer_name"));
            String storageDate = cursor.getString(cursor.getColumnIndex("storage_date"));
            String outDate = cursor.getString(cursor.getColumnIndex("out_date"));
            String modifyDate = cursor.getString(cursor.getColumnIndex("modify_date"));

            GoodsEntity goodsEntity = new GoodsEntity();
            goodsEntity.setId(id);
            goodsEntity.setName(name);
            goodsEntity.setReceivePerson(receivePerson);
            goodsEntity.setDescribe(describe);
            goodsEntity.setBuyerName(buyerName);
            goodsEntity.setStorageDate(storageDate);
            goodsEntity.setOutDate(outDate);
            goodsEntity.setModifyDate(modifyDate);
            goodsEntity.setChecked(false);
            list.add(goodsEntity);
        }
        db.close();
        return list;
    }
}
